package com.hdoit.apollo.web.cargo.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.model.Standard;
import com.hdoit.apollo.logical.cargo.model.StandardProperty;
import com.hdoit.apollo.logical.cargo.model.StandardPropertyUser;
import com.hdoit.apollo.logical.cargo.model.StandardUser;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.cargo.service.IStandardPropertyService;
import com.hdoit.apollo.logical.cargo.service.IStandardPropertyUserService;
import com.hdoit.apollo.logical.cargo.service.IStandardService;
import com.hdoit.apollo.logical.cargo.service.IStandardUserService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.viewmodel.cargo.CollocationSearch;
import com.hdoit.apollo.logical.viewmodel.cargo.StandardPropertySearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年05月04日 11:22:48
 * @comment
 */
@Controller
@RequestMapping(value = "/cargo/collocation/")
public class CollocationController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/cargo/collocation/";
    }

    @Autowired
    private ICollocationService collocationService;

    @Autowired
    private IStandardUserService standardUserService;
    
    @Autowired
    private IStandardService standardService;
    
    @Autowired
    private IStandardPropertyService standardPropertyService;
    
    @Autowired
    private IStandardPropertyUserService standardPropertyUserService;
    
    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="detailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String newDetailedViewName="newDetailed";
    private final String newSaveViewName="newSave";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CollocationSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(collocationService.queryCountMP(search));
        List<Collocation> collocations= collocationService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("collocations", collocations);
        model.addAttribute("collocationSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String collocationDetailed(Model model,String key){
        Collocation collocation=new Collocation();
        if(!StringUtils.isEmpty(key)){
            collocation=collocationService.queryModel(key);
        }
        model.addAttribute("collocation", collocation);
        return view(detailedViewName);
    }
    
    
    @RequestMapping(value = newDetailedViewName, method = { RequestMethod.GET})
    public String newDetailed(Model model,String key,String cargoId){
    	System.err.println(cargoId+"产品id");
    	List<StandardPropertyUser> standardPropertyUsers=new ArrayList<StandardPropertyUser>();
    	List<StandardUser> standardUsers=new ArrayList<StandardUser>();
    	List<Standard> standards=new ArrayList<Standard>();
        List<StandardProperty> standardProperties=new ArrayList<StandardProperty>();
    	
    	 StandardPropertySearch search=new StandardPropertySearch();
    	 search.setStandardId(key);
         standards=standardService.queryListMP(search,null);
         standardProperties=standardPropertyService.queryListMP(search,null);
         
         System.err.println(search.getStandardId()+"规格id");
         
    	 if(!StringUtils.isEmpty(cargoId)){
         	standardUsers=standardUserService.queryListByCargoId(cargoId);
         }
        Collocation collocation=new Collocation();
        if(!StringUtils.isEmpty(key)){
            collocation=collocationService.queryModel(key);
        }
        model.addAttribute("collocation", collocation);
        model.addAttribute("standardUsers", standardUsers);
        model.addAttribute("standardPropertyUsers", standardPropertyUsers);
        model.addAttribute("standardProperties", standardProperties);
        model.addAttribute("standards", standards);
        return view(newDetailedViewName);
    }
    @RequestMapping(value = newSaveViewName, method = { RequestMethod.POST})
    public String newSave(Model model,Collocation collocation){
    	Collocation  collocationed=new Collocation();
    	List<StandardPropertyUser> standardPropertyUsers=new ArrayList<StandardPropertyUser>();
    	
    	List<StandardProperty> standardProperties=new ArrayList<StandardProperty>();
    	
        if(StringUtils.isEmpty(collocation.getCollocationId())){
            collocation.setCollocationId(CommonUtils.getKeyId());
            collocation.setCreateDate(DateUtils.getCurrent());
            collocation.setDeleteMark(YesOrNoInteger.no);
            collocation.setCreateUserId(getLoginModel().getLoginId());
            collocation.setCreateUserName(getLoginModel().getLoginName());
            collocation.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            collocation.setShopId(shop.getShopId());
        }else{
        	collocationed=collocationService.queryModel(collocation.getCollocationId());
        	collocation.setCollocationId(collocationed.getCollocationId());
        	
        	standardPropertyUsers=standardPropertyUserService.queryListByCargoId(collocationed.getCargoId());
        	System.err.println(standardPropertyUsers.size()+"查出来么？");
				
					for (StandardPropertyUser standardPropertyUser : standardPropertyUsers) {
						standardPropertyUser.setModelStatus(ModelStatus.update);
						if (standardPropertyUser.getOtherPropertyName()!=null) {
							
							if (collocationed.getPropertyNameOne()!=null) {
								
								if (collocationed.getPropertyNameOne().equals(standardPropertyUser.getOtherPropertyName())) {						
									standardPropertyUser.setOtherPropertyName(collocation.getPropertyNameOne());	
									System.err.println("mei");
								}
							}
							if (collocationed.getPropertyNameTwo()!=null) {
								if (collocationed.getPropertyNameTwo().equals(standardPropertyUser.getOtherPropertyName())) {
									standardPropertyUser.setOtherPropertyName(collocation.getPropertyNameTwo());	
									System.err.println("lai");
								}
							}
							
						}else {
							standardPropertyUser.setModelStatus(ModelStatus.update);
							StandardProperty standardProperty=new StandardProperty();
							standardProperty=standardPropertyService.queryModel(standardPropertyUser.getPropertyId());
							if (collocationed.getPropertyNameOne()!=null) {
								if (collocationed.getPropertyNameOne().equals(standardProperty.getPropertyName())) {
									standardProperty.setPropertyName(collocation.getPropertyNameOne());
									standardProperty.setModelStatus(ModelStatus.update);
									standardProperties.add(standardProperty);
								}
							}
							if (collocationed.getPropertyNameTwo()!=null) {
								if (collocationed.getPropertyNameTwo().equals(standardProperty.getPropertyName())) {
									standardProperty.setPropertyName(collocation.getPropertyNameTwo());
									standardProperty.setModelStatus(ModelStatus.update);
									standardProperties.add(standardProperty);
								}
							}
							
						}
			}
        	
            collocation.setModifyDate(DateUtils.getCurrent());
            collocation.setModifyUserId(getLoginModel().getLoginId());
            collocation.setModifyUserName(getLoginModel().getLoginName());
            collocation.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        boolean result=false;
        result=collocationService.applyChange(collocation);
        System.err.println("不该");
        if (result) {
        	result=standardPropertyUserService.applyChanges(standardPropertyUsers);
        	System.err.println("改其他");
		}
        if (!StringUtils.isEmpty(standardProperties)&&standardProperties.size()>0) {
        	 if (result) {
     			result=standardPropertyService.applyChanges(standardProperties);
     			System.err.println("改属性");
     		}
		}
       
        
        if(result){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("collocation", collocation);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(newDetailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Collocation collocation){
        if(StringUtils.isEmpty(collocation.getCollocationId())){
            collocation.setCollocationId(CommonUtils.getKeyId());
            collocation.setCreateDate(DateUtils.getCurrent());
            collocation.setDeleteMark(YesOrNoInteger.no);
            collocation.setCreateUserId(getLoginModel().getLoginId());
            collocation.setCreateUserName(getLoginModel().getLoginName());
            collocation.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            collocation.setShopId(shop.getShopId());
        }else{
            collocation.setModifyDate(DateUtils.getCurrent());
            collocation.setModifyUserId(getLoginModel().getLoginId());
            collocation.setModifyUserName(getLoginModel().getLoginName());
            collocation.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(collocationService.applyChange(collocation)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("collocation", collocation);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(collocationService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}